package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.*;
import one.stand.request.AssembleRequest;
import one.stand.request.GoodSpecRequest;
import one.stand.request.GoodsAssembleRequest;
import one.stand.response.AssembleResponse;
import one.stand.response.GoodsAssembleResponse;
import one.stand.service.AssembleService;
import one.stand.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class AssembleServiceImpl extends BaseService implements AssembleService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel<Boolean> insertGoodsAssemble(GoodsAssembleRequest request) {
        if(request == null || request.getGoodsId() == null){
            return ResultModel.checkParamFail();
        }
        if(request.getStartTimes() == null || request.getEndTimes() == null){
            return ResultModel.fail("请输入开团时间和结束时间~！");
        }
        if(request.getPeopleNum() == null || request.getMaxPurchase() == null || request.getMaxPurchase() == null || request.getJoinRegiment() == null){
            return ResultModel.fail("请输入完整拼团参数配置~！");
        }
        if(request.getGoodSpecRequestList() == null){
            return ResultModel.fail("请输入拼团金额和平台库存~！");
        }
        GoodsAssembleModel model = new GoodsAssembleModel();
        BeanUtils.copyProperties(request, model);
        try {
            model.setStartTime(formatter.parse(request.getStartTimes()));
            model.setEndTime(formatter.parse(request.getEndTimes()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 保存商品平台配置
        if(request.getAssembleId() != null){
            goodsAssembleMapper.updateByPrimaryKeySelective(model);
        }else{
            if(goodsAssembleMapper.getGoodsAssemble(request.getGoodsId()) != null){
                return ResultModel.fail("该商品已经有拼团配置~！");
            }
            model.setIsDel(0);
            model.setCreateTime(currentDate());
            goodsAssembleMapper.insertSelective(model);
        }
        // 修改商品规格拼团配置
        for(GoodSpecRequest request1 : request.getGoodSpecRequestList()){
            GoodSpecModel model1 = new GoodSpecModel();
            model1.setSpecId(request1.getSpecId());
            model1.setAssembleNum(request1.getAssembleNum());
            model1.setAssemblePrice(request1.getAssemblePrice());
            goodSpecMapper.updateByPrimaryKeySelective(model1);
        }
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<List<GoodsAssembleResponse>> selectGoodsAssembleDraftsPage(GoodsAssembleRequest request) {
        if(request == null || request.getCId() == null){
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        GoodsAssembleModel model = new GoodsAssembleModel();
        model.setCompanyId(request.getCId().intValue());
        model.setIsAssemble(0);
        List<GoodsAssembleModel> list = goodsAssembleMapper.selectGoodsAssembleDraftsPage(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        PageInfo<GoodsAssembleModel> pageInfo = new PageInfo<>(list);
        List<GoodsAssembleResponse> responseList = new ArrayList<>();
        pageInfo.getList().forEach(obj->{{
            GoodsAssembleResponse response = new GoodsAssembleResponse();
            BeanUtils.copyProperties(obj, response);
            responseList.add(response);
        }});
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Boolean> delGoodsAssembleInfo(GoodsAssembleRequest request) {
        if(request == null || request.getAssembleId() == null){
            return ResultModel.checkParamFail();
        }
        GoodsAssembleModel model = new GoodsAssembleModel();
        model.setAssembleId(request.getAssembleId());
        model.setIsDel(1);
        goodsAssembleMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> releaseGoodsAssemble(GoodsAssembleRequest request) {
        if(request == null || request.getGoodsId() == null){
            return ResultModel.checkParamFail();
        }
        // 修改商品拼团状态
        goodsMapper.updateByPrimaryKeySelective(new GoodsModel(){{
            setGoodsId(request.getGoodsId());
            setIsAssemble(1);
        }});
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<List<AssembleResponse>> selectGoodsAssemblePage(AssembleRequest request) {
        if(request == null || request.getCId() == null){
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        AssembleModel model = new AssembleModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId().intValue());
        List<AssembleModel> list = assembleMapper.selectGoodsAssemblePage(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        PageInfo<AssembleModel> pageInfo = new PageInfo<>(list);
        List<AssembleResponse> responseList = new ArrayList<>();
        pageInfo.getList().forEach(obj->{{
            AssembleResponse response = new AssembleResponse();
            BeanUtils.copyProperties(obj, response);
            response.setNickeName(StringUtils.isBlank(obj.getNickeName()) ? "" : new String(java.util.Base64.getDecoder().decode(obj.getNickeName())));
            responseList.add(response);
        }});
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<AssembleResponse> getGoodsAssembleInfo(AssembleRequest request) {
        if(request == null || request.getAssembleId() == null){
            return ResultModel.checkParamFail();
        }
        AssembleModel model = new AssembleModel();
        model.setAssembleId(request.getAssembleId());
        List<AssembleModel> list = assembleMapper.selectGoodsAssemblePage(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        AssembleResponse response = new AssembleResponse();
        BeanUtils.copyProperties(list.get(0), response);
        response.setGoodSpecModelList(goodSpecMapper.getSpec(new GoodSpecModel(){{
            setGoodsId(response.getGoodsId());
        }}));
        response.setOrdersModelList(assembleMapper.ordersModelList(new AssembleModel(){{
            setAssembleId(request.getAssembleId());
        }}));
        return ResultModel.success(response);
    }
}
